//
//  FxDBManager.m
//  FxHejinbo
//
//  Created by hejinbo on 15/5/13.
//  Copyright (c) 2015年 MyCos. All rights reserved.
//

#import "FxDBManager.h"
#import "FxDBManager+Private.h"
#import "NewsInfo.h"
#import "ColumnInfo.h"

@implementation FxDBManager

#define TableColumns          @"FxColumns"
#define TableNews             @"FxNews"
#define TableComments         @"FxComments"
#define ChacheComments        @"ChacheComments"


+ (void)saveColumns:(NSDictionary *)dictInfo
{
    NSString *dbFile = [FxGlobal getUserDBFile];
    
    //存储
    [FxDBManager save:dictInfo
           primaryKey:@"pageid"
              inTable:TableColumns
             inDBFile:dbFile];
    
    NSLog(@"%@",NSHomeDirectory());
}

+ (NSArray *)fetchColumns:(NSString *)pageID
{
    
    NSString *dbFile = [FxGlobal getUserDBFile];
    NSDictionary *dict = @{@"pageid":pageID};
    NSArray *fields = @[@"pageid",@"json"];
    NSArray *contents = [FxDBManager fetchWithCondition:dict
                                              forFields:fields
                                                inTable:TableColumns
                                               inDBFile:dbFile];
    
    if (contents.count > 0) {
        dict = [contents objectAtIndex:0];
        dict = [FxJsonUtility jsonValueFromString:[dict objectForKey:@"json"]];
        return [ColumnInfo arrayFromDict:dict];
    }
    
    return nil;
}

+ (void)saveNews:(NSDictionary *)dictInfo
{
    NSString *dbFile = [FxGlobal getUserDBFile];
    
    //存储
    [FxDBManager save:dictInfo
           primaryKey:@"pi"
              inTable:TableNews
             inDBFile:dbFile];
}

+ (NSArray *)fetchNews
{
    NSString *dbFile = [FxGlobal getUserDBFile];
    NSMutableArray *mArray = [[NSMutableArray alloc]init];
    NSDictionary *dict = nil;
    NSArray *fields = @[@"b", @"pi"];
    NSArray *contents = [FxDBManager fetchWithCondition:dict
                         forFields:fields
                         inTable:TableNews
                         inDBFile:dbFile];
    
    if (contents.count > 0) {
        for (NSDictionary *dictInfo in contents) {
            [mArray addObject:[CommentInfo infoFromDict:dictInfo]];
        }
        return mArray;
    }

    
    return nil;
}

+ (NSString *)fetchNews:(NSString*)commentid
{
    NSString *dbFile = [FxGlobal getUserDBFile];
    NSString *jsonNews = nil;
    NSDictionary *dict = @{@"pi":commentid};
    NSArray *fields = @[@"json"];
    NSArray *contents = [FxDBManager fetchWithCondition:dict
                                              forFields:fields
                                                inTable:TableNews
                                               inDBFile:dbFile];
    
    if (contents.count > 0) {
        for (NSDictionary *dictInfo in contents) {
            jsonNews = [dictInfo valueForKey:@"json"];
        }
        return jsonNews;
    }
    
    
    return nil;
}


+ (void)delectNews:(NSString *)commentid
{
    NSString *dbFile = [FxGlobal getUserDBFile];
    NSDictionary *dict = @{@"pi":commentid};
    [FxDBManager deleteWithCondition:dict inTable:TableNews inDBFile:dbFile];
    
}


+ (void)saveComments:(NSDictionary *)dictInfo
{
    NSString *dbFile = [FxGlobal getUserDBFile];
    
    //存储
    [FxDBManager save:dictInfo
           primaryKey:@"commentsid"
              inTable:TableComments
             inDBFile:dbFile];
    
    NSLog(@"%@",NSHomeDirectory());
}

+ (CommentInfo *)fetchComments:(NSString *)commentsID
{
    NSString *dbFile = [FxGlobal getUserDBFile];
    NSDictionary *dict = @{@"commentsid":commentsID};
    NSArray *fields = @[@"commentsid",@"json"];
    NSArray *contents = [FxDBManager fetchWithCondition:dict
                                              forFields:fields
                                                inTable:TableComments
                                               inDBFile:dbFile];
    
    if (contents.count > 0) {
        dict = [contents objectAtIndex:0];
        dict = [FxJsonUtility jsonValueFromString:[dict objectForKey:@"json"]];
        return [CommentInfo infoFromDict:dict];
    }
    
    return nil;
}

+ (NSArray *)fetchComments
{
    NSString *dbFile = [FxGlobal getUserDBFile];
    NSDictionary *dict = nil;
    NSArray *fields = @[@"commentsid",@"json"];
    NSMutableArray *mArray = [NSMutableArray array];
    NSArray *contents = [FxDBManager fetchWithCondition:dict
                                              forFields:fields
                                                inTable:TableComments
                                               inDBFile:dbFile];
  if (contents.count > 0) {
       for (NSDictionary *dictInfo in contents) {
          dict = [FxJsonUtility jsonValueFromString:[dictInfo objectForKey:@"json"]];
          [mArray addObject:[CommentInfo infoFromDict:dict]];
    }
    return mArray;
  }
    return nil;
}

+ (void)delectCommnets:(NSString *)commentsID
{
    NSString *dbFile = [FxGlobal getUserDBFile];
    NSDictionary *dict = @{@"commentsid":commentsID};
    [FxDBManager deleteWithCondition:dict inTable:TableComments inDBFile:dbFile];

}

+ (void)saveChacheComments:(NSDictionary *)dictInfo
{
    NSString *dbFile = [FxGlobal getUserDBFile];
    
    //存储
    [FxDBManager save:dictInfo
           primaryKey:@"commentsid"
              inTable:ChacheComments
             inDBFile:dbFile];
    
    NSLog(@"%@",NSHomeDirectory());
}


+ (NSArray *)fetchChacheComments:(NSString *)commentsID
{
    //T1348647853363
    NSString *dbFile = [FxGlobal getUserDBFile];
    NSDictionary *dict = @{@"chachecommentid":commentsID};
    NSArray *fields = @[@"json"];
    NSMutableArray *mArray = [NSMutableArray array];
    NSArray *contents = [FxDBManager fetchWithCondition:dict
                                              forFields:fields
                                                inTable:ChacheComments
                                               inDBFile:dbFile];
    
    if (contents.count > 0) {
        for (NSDictionary *dictInfo in contents) {
            dict = [FxJsonUtility jsonValueFromString:[dictInfo objectForKey:@"json"]];
            [mArray addObject:[CommentInfo infoFromDict:dict]];
        }
        return mArray;
    }
    
    return nil;
}

+ (NSArray *)fetchChacheComments
{
    NSString *dbFile = [FxGlobal getUserDBFile];
    NSDictionary *dict = nil;
    NSArray *fields = @[@"date"];
    NSMutableArray *mArray = [NSMutableArray array];
    NSArray *contents = [FxDBManager fetchWithCondition:dict
                                              forFields:fields
                                                inTable:ChacheComments
                                               inDBFile:dbFile];
    
    if (contents.count > 0) {
        for (NSDictionary *dictInfo in contents) {
            [mArray addObject:[dictInfo valueForKey:@"date"]];
        }
        return mArray;
    }
    
    return nil;
}


+ (void)delectChacheCommnets:(NSString *)Date
{
    NSString *dbFile = [FxGlobal getUserDBFile];
    NSDictionary *dict = @{@"date":Date};
    [FxDBManager deleteWithCondition:dict inTable:ChacheComments inDBFile:dbFile];
    
}


@end
